package day04.byteBuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/**
 * @author chenshijie
 * @date 2023/12/18 15:43
 */

public class ByteBufTest {
    public static void main(String[] args) {
        // 初始化一个容量为9，最大容量为100的buffer
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(9, 100);
//        print("allocate ByteBuf(9, 100)", buffer);
//         capacity(): 9
//         maxCapacity(): 100
//         readerIndex(): 0
//         readableBytes(): 0
//         isReadable(): false
//         writerIndex(): 0
//         writableBytes(): 9
//         isWritable(): true
//         maxWritableBytes(): 100


        // write方法改变写指针，写完之后写指针未到capacity的时候，buffer仍然可写
        buffer.writeBytes(new byte[]{1, 2, 3, 4});
//        print("writeBytes(1, 2, 3, 4)", buffer);
//         capacity(): 9
//         maxCapacity(): 100
//         readerIndex(): 0
//         readableBytes(): 4
//         isReadable(): true
//         writerIndex(): 4
//         writableBytes(): 5
//         isWritable(): true
//         maxWritableBytes(): 96


        // write方法改变写指针，写完之后写指针未到capacity的时候，buffer仍然科协，写完int类型之后，写指针增加4
        buffer.writeInt(12);
//        print("writeInt(12)", buffer);
//         capacity(): 9
//         maxCapacity(): 100
//         readerIndex(): 0
//         readableBytes(): 8
//         isReadable(): true
//         writerIndex(): 8
//         writableBytes(): 1
//         isWritable(): true
//         maxWritableBytes(): 92


        // write方法改变写指针，写的时候写指针等于capacity的时候，buffer不可写
        buffer.writeBytes(new byte[]{5});
//        print("writeBytes(5)", buffer);
//        capacity(): 9
//        maxCapacity(): 100
//        readerIndex(): 0
//        readableBytes(): 9
//        isReadable(): true
//        writerIndex(): 9
//        writableBytes(): 0
//        isWritable(): false
//        maxWritableBytes(): 91

        // write方法改变写指针，写的时候发现buffer不可写则开始扩容，扩容之后capacity随即改变，扩容到了64
        buffer.writeBytes(new byte[]{6});
//        print("writeBytes(6)", buffer);
//        capacity(): 64
//        maxCapacity(): 100
//        readerIndex(): 0
//        readableBytes(): 10
//        isReadable(): true
//        writerIndex(): 10
//        writableBytes(): 54
//        isWritable(): true
//        maxWritableBytes(): 90

        // get方法不改变读写指针
        System.out.println("getByte(3) return: " + buffer.getByte(3));
        System.out.println("getShort(3) return: " + buffer.getShort(3));
        System.out.println("getInt(3) return: " + buffer.getInt(3));
//        print("getBytes()", buffer);
//        capacity(): 64
//        maxCapacity(): 100
//        readerIndex(): 0
//        readableBytes(): 10
//        isReadable(): true
//        writerIndex(): 10
//        writableBytes(): 54
//        isWritable(): true
//        maxWritableBytes(): 90

        // set方法不改变读写指针
        System.out.println("getByte(3) return: " + buffer.getByte(3));
        // 改变位置的值
        buffer.setByte(3, 7);
        System.out.println("getByte(3) return: " + buffer.getByte(3));
//        print("setByte()", buffer);
//        capacity(): 64
//        maxCapacity(): 100
//        readerIndex(): 0
//        readableBytes(): 10
//        isReadable(): true
//        writerIndex(): 10
//        writableBytes(): 54
//        isWritable(): true
//        maxWritableBytes(): 90

        // read方法改变指针
        byte[] dst = new byte[buffer.readableBytes()];
        buffer.readBytes(dst);
        print("readBytes(" + dst.length + ")", buffer);
//        capacity(): 64
//        maxCapacity(): 100
//        readerIndex(): 10
//        readableBytes(): 0
//        isReadable(): false
//        writerIndex(): 10
//        writableBytes(): 54
//        isWritable(): true
//        maxWritableBytes(): 90
    }

    private static void print(String action, ByteBuf buffer) {
        System.out.println("after ========== " + action + " ============== ");
        System.out.println("capacity(): " + buffer.capacity());
        System.out.println("maxCapacity(): " + buffer.maxCapacity());
        System.out.println("readerIndex(): " + buffer.readerIndex());
        System.out.println("readableBytes(): " + buffer.readableBytes());
        System.out.println("isReadable(): " + buffer.isReadable());
        System.out.println("writerIndex(): " + buffer.writerIndex());
        System.out.println("writableBytes(): " + buffer.writableBytes());
        System.out.println("isWritable(): " + buffer.isWritable());
        System.out.println("maxWritableBytes(): " + buffer.maxWritableBytes());
    }
}
